package com.micangcaihang.common.http;


import com.micangcaihang.common.http.exception.ApiException;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.ObservableSource;
import io.reactivex.ObservableTransformer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;

/**
 * des:对服务器返回数据成功和失败处理
 * Created by xsf
 * on 2016.09.9:59
 */

/**************使用例子******************/
/*_apiService.login(mobile, verifyCode)
        .compose(RxSchedulersHelper.io_main())
        .compose(RxResultHelper.handleResult())
        .//省略*/

public class RxHelper {
//    /**
//     * 对服务器返回数据进行预处理
//     *
//     * @param <T>
//     * @return
//     */
//    public static <T> ObservableTransformer<ApiResult<T>, ApiResult<T>> handleResult() {
//        return new ObservableTransformer<ApiResult<T>, ApiResult<T>>() {
//            @Override
//            public ObservableSource<ApiResult<T>> apply(@NonNull Observable<ApiResult<T>> upstream) {
//                return upstream.retryWhen(new RetryWhenException(3, 5000))
//                        .flatMap(new Function<ApiResult<T>, ObservableSource<ApiResult<T>>>() {
//                            @Override
//                            public ObservableSource<ApiResult<T>> apply(@NonNull ApiResult<T> tApiResult) throws Exception {
//                                if (tApiResult.success()) {
//                                    return createData(tApiResult);
//                                } else {
//                                    return Observable.error(new ApiException(tApiResult.status, tApiResult.message));
//                                }
//
//                            }
//                        }).subscribeOn(Schedulers.io())
//                        .observeOn(AndroidSchedulers.mainThread());
//            }
//        };
//
//    }

    /**
     * 对服务器返回数据进行预处理
     *
     * @param <T>
     * @return
     */
    public static <T> ObservableTransformer<ApiResult<T>, ApiResult<T>> handleResult() {
        return new ObservableTransformer<ApiResult<T>, ApiResult<T>>() {
            @Override
            public ObservableSource<ApiResult<T>> apply(@NonNull Observable<ApiResult<T>> upstream) {
                return upstream.
                        flatMap(new Function<ApiResult<T>, ObservableSource<ApiResult<T>>>() {
                            @Override
                            public ObservableSource<ApiResult<T>> apply(@NonNull ApiResult<T> tApiResult) throws Exception {
                                if (tApiResult.success()) {
                                    return createData(tApiResult);
                                } else {
                                    return Observable.error(new ApiException(tApiResult.status, tApiResult.message));
                                }

                            }
                        }).subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread());
            }
        };

    }

    /**
     * 创建成功的数据
     *
     * @param data
     * @param <T>
     * @return
     */
    private static <T> Observable<T> createData(final T data) {
        return Observable.create(new ObservableOnSubscribe<T>() {
            @Override
            public void subscribe(@NonNull ObservableEmitter<T> e) throws Exception {
                try {
                    e.onNext(data);
                    e.onComplete();
                } catch (Exception ex) {
                    e.onError(ex);
                }
            }
        });

    }
}
